<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of fichiers_m
 *
 * @author QUYET
 */
class Fichiers_m extends Model {

    function Fichiers() {
        parent::Model();
        $this->load->database();
    }
    function get_all_fitchiers($per_page, $off_set){
        $this->db->select();
        $this->db->from('pro_files');
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }
    function get_file_name($id){
        $this->db->select('pro_files.fichier');
        $this->db->from('pro_files');
        $this->db->where(array('id'=>$id));
        $query = $this->db->get();
        return $query;
    }
    function delete_file($id){
        if($this->db->delete('pro_files',array('id'=>$id))>0){
            $this->db->delete('pro_dispo',array('file_id'=>$id));
            $this->db->delete('pro_offer',array('file_id'=>$id));
            $this->db->delete('pro_plv',array('file_id'=>$id));
        }
    }
    function list_pro_offer($per_page, $off_set) {
        $this->db->select('pro_offer.id,pro_files.nom as nom_file,pro_files.fichier,pro.nom as nom_pro');
        $this->db->from('pro_offer  ,pro_files ,pro');
        $this->db->where('pro_offer.file_id = pro_files.id');
        $this->db->where('pro_offer.pro_id = pro.id');
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function list_pro_dispo($per_page, $off_set) {
        $this->db->select('pro_dispo.id,pro_files.nom as nom_file,pro_files.fichier,pro.nom as nom_pro');
        $this->db->from('pro_dispo  ,pro_files ,pro');
        $this->db->where('pro_dispo.file_id = pro_files.id');
        $this->db->where('pro_dispo.pro_id = pro.id');
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_pro_offer() {
        $this->db->select('pro_offer.id,pro_files.nom as nom_file,pro_files.fichier,pro.nom as nom_pro');
        $this->db->from('pro_offer ,pro_files ,pro');
        $this->db->where('pro_offer.file_id = pro_files.id');
        $this->db->where('pro_offer.pro_id = pro.id');
        $query = $this->db->get();
        return count($query->result_array()); //$this->db->count_all_results();
    }

    function count_pro_dispo() {
        $this->db->select('pro_dispo.id,pro_files.nom as nom_file,pro_files.fichier,pro.nom as nom_pro');
        $this->db->from('pro_dispo ,pro_files ,pro');
        $this->db->where('pro_dispo.file_id = pro_files.id');
        $this->db->where('pro_dispo.pro_id = pro.id');
        $query = $this->db->get();
        return count($query->result_array()); //$this->db->count_all_results();
    }

    function add_pro_offer($data) {
        return $this->db->insert('pro_offer', $data);
        print_r($data);
        die;
    }

    function add_pro_dispo($data) {
        return $this->db->insert('pro_dispo', $data);
    }

    function add_pro_files($data) {
        return $this->db->insert('pro_files', $data);
    }

    function get_new_pro_files_id() {
        //select MAX(id) as newID from pro_files
        $this->db->select('MAX(id) as newID');
        $this->db->from('pro_files');
        $query = $this->db->get();
        return $query;
    }

    function delete_pro_offer($id) {
        return $this->db->delete('pro_offer', array('id' => $id));
    }

    function delete_pro_dispo($id) {
        return $this->db->delete('pro_dispo', array('id' => $id));
    }

    function update_pro_offer($data, $id) {
        return $this->db->update('pro_offer', $data, array('id' => $id));
    }

    function update_pro_dispo($data, $id) {
        return $this->db->update('pro_dispo', $data, array('id' => $id));
    }

    function get_pro_offer_by_id($id) {
        $this->db->select('pro_offer.id,pro_offer.pro_id,pro_files.fichier');
        $this->db->from('pro_offer ,pro_files');
        $this->db->where('pro_offer.file_id = pro_files.id');
        $this->db->where(array('pro_offer.id' => $id));
        $query = $this->db->get();
        return $query;
    }

    function get_pro_dispo_by_id($id) {
        $this->db->select('pro_dispo.id,pro_dispo.pro_id,pro_files.fichier');
        $this->db->from('pro_dispo ,pro_files');
        $this->db->where('pro_dispo.file_id = pro_files.id');
        $this->db->where(array('pro_dispo.id' => $id));
        $query = $this->db->get();
        return $query;
    }

    function get_all_plv_pro($per_page, $off_set) {
        $this->db->select('pro_plv.id,pro_files.nom as nom_file,pro_files.fichier,pro.nom as nom_pro');
        $this->db->from('pro_plv  ,pro_files ,pro');
        $this->db->where('pro_plv.file_id = pro_files.id');
        $this->db->where('pro_plv.pro_id = pro.id');
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_all_plv_pro_cat($per_page, $off_set) {
        $this->db->select();
        $this->db->from('plv_pro_cat');
        $this->db->limit($per_page, $off_set);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_pro_plv_cat_by_id($id) {
        $this->db->select();
        $this->db->from('plv_pro_cat');
        $this->db->where(array('id' => $id));
        $query = $this->db->get();
        return $query;
    }

    function count_all_pro_plv() {
        $this->db->select('pro_plv.id,pro_files.nom as nom_file,pro_files.fichier,pro.nom as nom_pro');
        $this->db->from('pro_plv  ,pro_files ,pro');
        $this->db->where('pro_plv.file_id = pro_files.id');
        $this->db->where('pro_plv.pro_id = pro.id');
        $query = $this->db->get();
        return count($query->result_array());
    }

    function delete_pro_plv($id) {
        return $this->db->delete('pro_plv', array('id' => $id));
    }

    function delete_pro_plv_cat($id) {
        return $this->db->delete('plv_pro_cat', array('id' => $id));
    }

    function add_pro_plv($data) {
        return $this->db->insert('pro_plv', $data);
    }

    function add_pro_plv_cat($data) {
        return $this->db->insert('plv_pro_cat', $data);
    }

    function get_pro_plv_by_id($id) {
        $this->db->select('pro_plv.id,pro_plv.pro_id,pro_plv.plv_cat,pro_files.fichier');
        $this->db->from('pro_plv ,pro_files');
        $this->db->where('pro_plv.file_id = pro_files.id');
        $this->db->where(array('pro_plv.id' => $id));
        $query = $this->db->get();
        return $query;
    }

    function update_pro_plv($id, $data) {
        return $this->db->update('plv_pro_cat', $data, array('id' => $id));
    }

}

?>
